package com.urbo.android.shortcutter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;


/**
 * AlertDialog wrapper. Create dialog from given layout resource with OK and cancel buttons and set the click listeners 
 * @author radovan.urban@gmail.com
 *
 */
public class UrboDialog {

	protected AlertDialog mUrboDialog;			// Activity dialog after item click;
	protected View mUrboDialogLayout;			// Reference to view hosted in the activityDialog
	protected Context mContext;				
	protected int mLayoutResourceId;			// Resource id of layout to display
	protected int mRequestCode;				// This code is returned by dismiss(). Useful for more dialogs with one OK listener
	
	public UrboDialog(Context context, int layoutRecourceId, DialogInterface.OnClickListener dialogClickListener) {
		this.mContext = context;
		this.mLayoutResourceId = layoutRecourceId;
		createDialog(dialogClickListener);
	}
	
	/**
	 * Create AlertDialog  from layout layoutRecourceId
	 * Set OK and Cancel button listeners
	 */
	private void createDialog(DialogInterface.OnClickListener dialogClickListener) {
		AlertDialog.Builder builder;

		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mUrboDialogLayout = inflater.inflate(mLayoutResourceId, null);

		builder = new AlertDialog.Builder(mContext);
		builder.setView(mUrboDialogLayout);
		builder.setPositiveButton(R.string.text_ok, dialogClickListener);
		builder.setNegativeButton(R.string.text_cancel, dialogClickListener);
		mUrboDialog = builder.create();
		mUrboDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}
	
	
	
	public void show(int requestCode) {
		this.mRequestCode = requestCode;
		mUrboDialog.show();
	}
	
	public int dismiss() {
		mUrboDialog.dismiss();
		return mRequestCode;
	}
	
	
}
